package logs

import (
	"log"
	"os"
)

const (
	LOG_FILE      = "./" + "message" + ".log"
	MAX_FILE_SIZE = 200 * 1024 * 1024 //最大100M
)

func init() {
	logFile, err := os.OpenFile(LOG_FILE, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err != nil {
		panic(err)
	}
	log.SetOutput(logFile) // 将文件设置为log输出的文件
	log.SetPrefix("[gateway]")

	log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
}

//打印Debug信息
func Debug(format string, v ...any) {
	clearLog()
	log.Printf("[DEBUG]:"+format, v)
}

//打印info 信息
func Info(format string, v ...any) {
	clearLog()
	log.Printf("[INFO]:"+format, v)
}

//打印错误信息
func Error(format string, v ...any) {
	clearLog()
	log.Printf("[ERROR]:"+format, v)
}

//清除日志内容
func clearLog() {
	fi, err := os.Stat(LOG_FILE)
	if err != nil {
		log.Fatalf("[FATAL]:%v", err)
	}
	if fi.Size() >= MAX_FILE_SIZE {
		os.Truncate(LOG_FILE, 0)
	}
}
